﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class BoardUI : MonoBehaviour {
    public GameObject CrossPrefab;
    public GameObject BlackPrefab;
    public GameObject WhitePrefab;
    public GameObject ChessFlag;
    public GameObject btnBack;

    private GameObject flag;
    private static Dictionary<Point, CrossUI> CrossList = new Dictionary<Point, CrossUI>();
    
    void Start()
    {
        AudioManager.SetMusicOn(true);
        AudioManager.SetSoundOn(true);
        AutoPlayMic();
        
        Datacenter.OnMapDataChanged += OnMapDataChanged;
        Datacenter.OnGameReplay += ResetData;
        for (int x = 0; x < Constant.CrossCount; x++)
        {
            for (int y = 0; y < Constant.CrossCount; y++)
            {
                GameObject crossObject = GameObject.Instantiate(CrossPrefab);
                crossObject.transform.SetParent(gameObject.transform);
                crossObject.transform.localScale = Vector3.one;

                Vector3 pos = crossObject.transform.localPosition;
                pos.x = x * Constant.GridSize;
                pos.y = y * Constant.GridSize;
                pos.z = 1;
                crossObject.transform.localPosition = pos;

                CrossUI cross = crossObject.GetComponent<CrossUI>();
                Point pt = new Point(x, y);
                cross.SetPosition(pt);
                CrossList.Add(pt, cross);
            }
        }
    }

    void OnMapDataChanged(Point pt, CrossVal val)
    {
        if (pt.x < 0 || pt.x > Constant.CrossCount)
            return;
        if (pt.y < 0 || pt.y > Constant.CrossCount)
            return;

        if (val == CrossVal.nil)
        {   //删除棋子
            if(CrossList.ContainsKey(pt))
            {
                CrossUI crossUI = CrossList[pt];
                if (crossUI.transform.childCount > 0)
                    GameObject.Destroy(crossUI.transform.GetChild(0).gameObject);
            }
        }
        else
        {
            //放置棋子
            if (CrossList.ContainsKey(pt))
            {
                GameObject newCress = GameObject.Instantiate(val == CrossVal.black ? BlackPrefab : WhitePrefab);
                newCress.transform.SetParent(CrossList[pt].transform, false);
                if (flag == null)
                {
                    flag = GameObject.Instantiate(ChessFlag);
                }
                flag.transform.SetParent(newCress.transform);
                flag.transform.localPosition = new Vector3(0, 0, 1);
                flag.transform.localScale = Vector3.one;
                AudioManager.PlaySound("move");
            }
        }
    }

    void ResetData()
    {
        foreach(var v in CrossList)
        {
            if (v.Value.transform.childCount > 0)
            {
                GameObject.Destroy(v.Value.transform.GetChild(0).gameObject);
            }
        }

        AutoPlayMic();

        AudioManager.PlaySound("begin");
    }

    string currentMusicName = "";
    System.Random random = new System.Random(100);
    void AutoPlayMic()
    {
        string name = "BGM_" + random.Next(1, 3);
        if (currentMusicName != name)
        {
            currentMusicName = name;
            AudioManager.PlayMusic(name);
        }
    }
}
